﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data.SqlClient;
using System.Data;
using prjKQXS_DLL;

namespace prjKQXS_Report.DMQUY
{
    public partial class rptQuy_Thang : DevExpress.XtraReports.UI.XtraReport
    {
        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;

        dsReports dsReports1 = new dsReports();

        public rptQuy_Thang()
        {
            InitializeComponent();
        }

        private void rptdmquythang_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            GroupFooter1.Visible = pMaDV.Value.ToString().Trim() == "%";

            lblDonvi.Text = "Phòng XSKT " + pTenDV.Value.ToString().Trim();           
            xrlblThang.Text = "Tháng " + ((DateTime)pNgay.Value).Month.ToString() + " năm " + ((DateTime)pNgay.Value).Year.ToString();

            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            dsReports1.Tables.Remove("DMQUY1");
            dsReports1.Tables.Add(xuly());
            this.DataSource = dsReports1;
            this.DataMember = "DMQUY1";
        }

        private void rptdmquythang_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }   

        int stt = 0;
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrtcSTT.Text = (++stt).ToString();
        }

        private DataTable xuly()
        {
            decimal tongsothu = 0;
            decimal tongsochi = 0;
            decimal tongso = 0;
            string sql = @"Select 
                                Ngay
                                , Ngaychungtu
                                , Sophieuthu
                                , Sophieuchi
                                , Noidung
                                , Sotienthu
                                , Sotienchi
                                , Sotienthu- Sotienchi AS Sodu
                                , Madv
                                , D.Ten_DT AS TenDV
                            from DMQUY Q INNER JOIN DMDONVI D ON D.Ma_DT = Q.Madv
                            Where D.isPublic = 1 
                                and rtrim(Madv) like @MaDV 
                                and MONTH(Ngay) = MONTH(@Ngay) and YEAR(Ngay) = YEAR(@Ngay)";

            SqlConnection conn = new SqlConnection(BienTc.connString);
            if (conn.State == ConnectionState.Closed) conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            da.SelectCommand.Parameters.AddWithValue("@Ngay", ((DateTime)pNgay.Value).Date);
            da.SelectCommand.Parameters.AddWithValue("@MaDV", pMaDV.Value.ToString().Trim());

            DataTable dt = new DataTable("DMQUY1");
            da.Fill(dt);

            if (dt.Rows.Count > 0)
                {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Sotienthu"] != null && dr["Sotienthu"].ToString().Trim() != "" && (decimal)clsCommon.Parse(dr["Sotienthu"].ToString().Trim(), 3) != 0)
                    {
                        tongsothu += (decimal)clsCommon.Parse(dr["Sotienthu"].ToString().Trim(), 3);
                    }
                    if (dr["Sotienchi"] != null && dr["Sotienchi"].ToString().Trim() != "" && (decimal)clsCommon.Parse(dr["Sotienchi"].ToString().Trim(), 3) != 0)
                    {
                        tongsochi += (decimal)clsCommon.Parse(dr["Sotienchi"].ToString().Trim(), 3);
                    }                
                }               
                }
                tongso = tongsothu - tongsochi;
                //lbltonquy.Text = tongso.ToString();
                lbltonquy.Text = String.Format("{0:0,0.000}", double.Parse(tongso.ToString()));
            return dt;
        }
    }
}
